home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / fdopen.c < prev    next >
C/C++ Source or Header  |  1994-01-15  |  1KB  |  72 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7.  
  8. extern int __mint;
  9.  
  10. FILE *fdopen(h, mode)
  11.     register int h;
  12.     register const char *mode;
  13. {
  14.     extern int __default_mode__;    /* see defmode.c */
  15.     register int i, iomode = 0, f = __default_mode__;
  16.     register FILE *fp = NULL;
  17.     void _getbuf __PROTO((FILE *));
  18.  
  19.     for(i=0; (!fp && (i < _NFILE)); ++i)
  20.         if(!(_iob[i]._flag & (_IORW | _IOREAD | _IOWRT)))
  21.             fp = &_iob[i];    /* empty slot */
  22.     if(!fp) {
  23.         errno = EMFILE;
  24.         return(NULL);
  25.     }
  26.  
  27.     while(*mode) {
  28.         switch(*mode++) {
  29.             case 'r':
  30.                 f |= _IOREAD;
  31.                 break;
  32.             case 'w':
  33.                 f |= _IOWRT;
  34.                 iomode |= (O_CREAT | O_TRUNC);
  35.                 break;
  36.             case 'a':
  37.                 f |= _IOWRT;
  38.                 iomode |= (O_CREAT | O_APPEND);
  39.                 break;
  40.             case '+':
  41.                 f &= ~(_IOREAD | _IOWRT);
  42.                 f |= _IORW;
  43.                 break;
  44.             case 'b':
  45.                 f |= _IOBIN;
  46.                 break;
  47.             case 't':
  48.                 f &= ~_IOBIN;
  49.                 break;
  50.             default:        /* illegal file mode */
  51.                 return(NULL);
  52.             }
  53.     }
  54.     if((i = (f & (_IORW | _IOREAD | _IOWRT))) == 0)
  55.         return(NULL);
  56.     else if(i == _IOREAD)
  57.         iomode |= O_RDONLY;
  58.     else if(i == _IOWRT)
  59.         iomode |= O_WRONLY;
  60.     else
  61.         iomode |= O_RDWR;
  62.  
  63.     if(isatty(h))
  64.         f |= __mint ? (_IODEV | _IONBF | _IOBIN) : (_IODEV | _IONBF);
  65.     else
  66.         f |= _IOFBF;
  67.     fp->_file = h;            /* file handle */
  68.     fp->_flag = f;            /* file status flags */
  69.     _getbuf(fp);
  70.     return(fp);
  71. }
  72.